package com.abc.robot01.activity.compose

import android.os.Bundle
import android.util.Log
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.RepeatMode
import androidx.compose.animation.core.animateFloatAsState
import androidx.compose.animation.core.animateIntOffsetAsState
import androidx.compose.animation.core.animateSizeAsState
import androidx.compose.animation.core.repeatable
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandVertically
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.scaleIn
import androidx.compose.animation.scaleOut
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.slideIn
import androidx.compose.animation.slideInHorizontally
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOut
import androidx.compose.animation.slideOutHorizontally
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ExperimentalLayoutApi
import androidx.compose.foundation.layout.FlowRow
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.offset
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.foundation.lazy.items
import androidx.compose.foundation.lazy.itemsIndexed
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.pager.HorizontalPager
import androidx.compose.foundation.pager.rememberPagerState
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.graphicsLayer
import androidx.compose.ui.platform.LocalDensity
import androidx.compose.ui.unit.IntOffset
import androidx.compose.ui.unit.dp
import androidx.constraintlayout.compose.ConstraintLayout
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlin.math.roundToInt
import kotlin.random.Random

//https://cs.android.com/androidx/platform/frameworks/support/+/androidx-main:compose/material3/material3/samples/src/main/java/androidx/compose/material3/samples/PullToRefreshSamples.kt

class ListAnimationSamples : ComponentActivity() {

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun AnimatedItemReordering() {
        val items = remember { (1..100).map { "Item $it" }.toMutableStateList() }
        LazyColumn {
            item{
                Text("List重排序")
            }

            itemsIndexed(items, key = { _, item -> item }) { index, item ->
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(16.dp)
                        .animateItemPlacement()
                ) {
                    Text(text = item)
                    Spacer(modifier = Modifier.weight(1f))
                    Button(onClick = {
                        if (index > 0) {
                            items.removeAt(index)
                            items.add(index - 1, item)
                        }
                    }) {
                        Text("Up")
                    }
                    Button(onClick = {
                        if (index < items.size - 1) {
                            items.removeAt(index)
                            items.add(index + 1, item)
                        }
                    }) {
                        Text("Down")
                    }
                }
            }
        }
    }

    @OptIn(ExperimentalFoundationApi::class)
    @Composable
    fun AnimatedItemRemoval() {
        val items = remember { (1..100).map { "Item $it" }.toMutableStateList() }
        Column {
            Button(onClick = { items.removeAt(0) }) {
                Text("remove first")
            }
            Button(onClick = {
                items.add(2,"new:"+  items.size)

            }) {
                Text("Add")
            }

            LazyColumn {
                items(items,key = { it }) { item ->
                    Text(
                        text = item,
                        modifier = Modifier
                            .fillMaxWidth()
                            .padding(16.dp)
                            .animateItemPlacement()
                    )
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Log.e("FirstFragment","MainActivity  onCreate");
        setContent {
            MaterialTheme {
                Surface {
                    Column( modifier = Modifier
                        .fillMaxSize()
//                        .verticalScroll(rememberScrollState())
                        .padding(8.dp),
                        verticalArrangement = Arrangement.spacedBy(8.dp)) {
                        AnimatedItemRemoval()
//                        AnimatedItemReordering()
//                        ConstraintLayoutWithGuidelines()
////                        SlideInFromLeftDemo()
//                        GraphicsLayer01()
//                        GraphicsLayer02()
//                        RepeatingRotationAnimation()
//                        AnimateSizeAsState01()
//                        Spring()
//                        Slide()
//                        Scale()
//                        HorizontalPager01()
//                        FlowRowSimpleUsageExample()
//                        BasicAnimatedVisibilityDemo()
//                        AnimatedContentSizeDemo()
//                        SimpleAnimatedContentSizeDemo()
//                        AnimatedVisibility01()
//                        Offset01()
//                        BackgroundAlphaAnim01()
//                        TextColorAnim01()
//                        AnimatedVisibility01()
//                        BoxWithBackgroundColor()
                    }
                }
            }
        }
    }

    companion object{
    }
}